package com.miniapp.web.controller.miniapp.api;

import com.alibaba.fastjson.JSONObject;
import com.miniapp.basics.entity.R;
import com.miniapp.common.core.controller.BaseController;
import com.miniapp.common.core.domain.model.LoginUser;
import com.miniapp.common.core.domain.model.miniapp.WxLoginBody;
import com.miniapp.common.exception.MiniAppException;
import com.miniapp.common.utils.SecurityUtils;
import com.miniapp.framework.web.service.MiniAppLoginService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/miniapp")
public class WxLoginController extends BaseController
{

    @Autowired
    private MiniAppLoginService miniAppLoginService;

    @PostMapping("/wxMiniLogin")
    public R wxMiniLogin(@RequestBody WxLoginBody wxLoginBody) throws MiniAppException
    {
        log.info("请求参数为：{},{}",wxLoginBody.getAppid(),wxLoginBody.getCode());
        JSONObject data = miniAppLoginService.wxMiniLogin(wxLoginBody);
        return R.success(data);
    }


    /**
     * 手机号、密码登录
     * @param wxLoginBody
     * @return
     * @throws MiniAppException
     */
    @PostMapping("/wxMiniLoginPassWord")
    public R wxMiniLoginPassWord(@RequestBody WxLoginBody wxLoginBody) throws MiniAppException
    {
        log.info("手机号密码登录，请求参数为：{}",JSONObject.toJSONString(wxLoginBody));
        JSONObject data = miniAppLoginService.wxMiniLoginPassWord(wxLoginBody);
        return R.success(data);
    }



    /**
     * 绑定手机号
     * @param wxLoginBody
     * @return
     */
    @PostMapping("/bindPhoneNumber")
    public R bindPhoneNumber(@RequestBody WxLoginBody wxLoginBody){
        LoginUser loginUser = getLoginUser();
        log.info("绑定手机号,请求参数为：{},{}",wxLoginBody.getAppid(),wxLoginBody.getCode());
        log.info("绑定手机号,获取到登录用户：{}",JSONObject.toJSONString(loginUser));
        JSONObject data = miniAppLoginService.bindPhoneNumber(wxLoginBody,loginUser);
        return R.success(data);
    }

}













